
getPublicModel = (model) => {
    try {
        let publicModel = require('./model/' + model + 'Model');
        return publicModel
    } catch (e) {
        let publicModel = require('./model/studentsModel');
        return publicModel
    }
}
// 登录
loginController = async (role, userNumber, passwd) => {
    let model = getPublicModel(role)
    let result = await model.findOne({ userNumber, passwd })
    return result;
}
// 注册
registerController = async (role, userNumber, passwd, name, gender, age) => {
    let model = getPublicModel(role)
    let results = await model.create({ userNumber, passwd, name, gender, age, state: "待审核", role })
    return {
        results
    }
}
// 获得数据列表-分页
getDataController = async (role, populate = {}, pageIndex, pageSize) => {
    let model = getPublicModel(role)
    // 求总数据 | 获得总数居个数
    let total = await model.count();
    // 求总页数
    let pageCount = Math.ceil(total / pageSize);
    // 得到分页后的数据
    let results;
    if (populate) {
        results = await model.find().populate(populate).skip((pageIndex - 1) * pageSize).limit(pageSize);
    } else {
        results = await model.find().skip((pageIndex - 1) * pageSize).limit(pageSize);
    }
    return {
        results,
        pageCount,
        total
    }
}
// 获得数据列表-不分页
getDataControllerUnPage = async (role, populate = {}) => {
    let model = getPublicModel(role)
    // 求总数据 | 获得总数居个数
    let total = await model.count();
    // 得到分页后的数据
    let results;
    if (populate) {
        results = await model.find().populate(populate);
    } else {
        results = await model.find();
    }
    return {
        results,
        total
    }
}
// 添加教师学生等角色
addUserController = async (role, userNumber, name, gender, age, phone, classId) => {
    let model = getPublicModel(role)
    let passwd = phone.substr(-6);
    let results = await model.create({ userNumber, name, gender, age, phone, passwd, classId, state: "待审核", role })
    return {
        results
    }
}
// 通用的 通过工号或者学号查询数据
userFindByIdController = async (role, userNumber) => { 
    let model = getPublicModel(role)
    let results = await model.findOne({ userNumber })
    return {
        results
    }
}
// 通用的 通过工号或者学号删除数据
delDataController = async (role, userNumber) => {
    let model = getPublicModel(role)
    let results = await model.deleteOne({ userNumber })
    return { results }
}

updateDataController = async (role, userNumber, name, gender, age, phone, classId) => {
    let model = getPublicModel(role)
    let results = await model.updateOne({ userNumber }, { name, gender, age, phone, classId })
    return { results }
}

openController = async (role, userNumber, state, unJoin = "") => {
    let model = getPublicModel(role)
    let results = await model.updateOne({ userNumber }, { state, unJoin })
    return { results }
}

// 获得待审核数据列表
getDataUnOpenController = async (role, populate = {}, pageIndex, pageSize) => {
    let model = getPublicModel(role)
    // 求总数据 | 获得总数居个数
    let countMid = await model.find({ state: "待审核" });
    let total = countMid.length;
    // 求总页数
    let pageCount = Math.ceil(total / pageSize);
    // 得到分页后的数据
    let results = await model.find({ state: "待审核" }).populate(populate).skip((pageIndex - 1) * pageSize).limit(pageSize);

    return {
        results,
        pageCount,
        total
    }
}
module.exports = {
    getPublicModel,
    loginController,
    userFindByIdController,
    getDataController,
    addUserController,
    delDataController,
    updateDataController,
    openController,
    getDataUnOpenController,
    getDataControllerUnPage
}